package cn.wolfcode.controller;

import cn.wolfcode.annotation.RequireLogin;
import cn.wolfcode.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.cache.service.IUserInfoRedisService;
import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Value("${sms.url}")
    private String smsUrl;
    @Value("sms.appkey")
    private String appKey;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        //判断手机号码是否存在
        try{
            userInfoService.sendVerifyCode(phone,smsUrl,appKey);
        }catch (Exception e){
            return new JsonResult<>(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
        }
        return JsonResult.success();
    }
//
//    phone: 13700000009
//    nickname: 1234
//    password: 1234
//    rpassword: 1234
//    verifyCode: e411
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode){
        try{
            userInfoService.regist(phone, nickname, password, rpassword,verifyCode);
        }catch (LogicException e){
            e.printStackTrace();
                return new JsonResult<>(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.defaultError();
        }
        return JsonResult.success();
    }
    @PostMapping("/login")
    public Object login(String username, String password){
        //用户登录成功
        UserInfo userInfo = userInfoService.login(username, password);

        //创建token添加到redis上
        String token = userInfoRedisService.setToken(userInfo);


        //页面需要 token  user
        HashMap<Object, Object> map = new HashMap<>();

        map.put("token",token);
        map.put("user",userInfo);

        return JsonResult.success(map);
    }
    @RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        //通过token从redis中获取用户信息
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        System.out.println(userInfo);

        return JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    public Object strategiesFavor(String userId){
        return JsonResult.success(strategyStatisVORedisService.getUserStrategyFavor(userId));
    }
}
